<template>
  <div>
    <h1>当前求和为{{ sum }}</h1>
    <h1>当前求和值放大十倍为：{{ bigSum }}</h1>
    <h3>我在{{ school }}，学习{{ subject }}</h3>
    <select v-model.number="number">
      <!-- 或者:value -->
      <option value="1">1</option>
      <option value="2">2</option>
      <option value="3">3</option>
    </select>
    <button @click="addNumber(number)">+</button>
    <button @click="reduceNumber(number)">-</button>
    <button @click="oddAddNumber(number)">当前的求和值为奇数再加</button>
    <button @click="delayAddNumber(number)">等500ms后再加</button>
  </div>
</template>

<script>
import { mapState, mapGetters, mapMutations, mapActions } from "vuex";
export default {
  name: "Count",
  data() {
    return {
      number: 1, //默认选中1
    };
  },
  methods: {
    //靠程序员自己手写的代码
    /* addNumber() {
      // this.$store.dispatch("jia", this.number);先与actions连接再与mutations连接
      this.$store.commit("JIA", this.number); //直接与mutations连接
    },
    reduceNumber() {
      // this.$store.dispatch("jian", this.number);先与actions连接再与mutations连接
      this.$store.commit("JIAN", this.number); //直接与mutations连接
    }, */

    /* ******************************************** */

    //借助mapMutations生成方法，方法中会调用commit去联系mutations(对象的写法)
    ...mapMutations({ addNumber: "JIA", reduceNumber: "JIAN" }), //但是生成的代码为addNumber(value) {this.$store.commit("JIA", value)，若模板中不传参数的话，value默认为事件对象e

    //借助mapMutations生成方法，方法中会调用commit去联系mutations(数组的写法)
    // ...mapMutations(["JIA","JIAN"]),

    /* ******************************************** */

    //靠程序员自己手写的代码
    /* oddAddNumber() {
      this.$store.dispatch("oddjia", this.number);
    },
    delayAddNumber() {
      this.$store.dispatch("delayjia", this.number);
    }, */

    /* ******************************************** */

    //借助mapActions生成方法，方法中会调用dispatch去联系actions(对象的写法)
    ...mapActions({ oddAddNumber: "oddjia", delayAddNumber: "delayjia" }), //但是生成的代码为oddAddNumber(value) {this.$store.dispatch("oddjia", value)，若模板中不传参数的话，value默认为事件对象e

    //借助mapActions生成方法，方法中会调用dispatch去联系actions(数组的写法)
    // ...mapActions(["oddjia", "delayjia"]),
  },
  computed: {
    ...mapState({ sum: "sum", school: "school", subject: "subject" }),
    ...mapGetters(["bigSum"]),
  },
};
</script>

<style scoped>
button {
  margin: 0 5px 0 5px;
}
</style>